﻿using AdinZ.Web.UI;
using Novel.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/5 22:39:07
* */
namespace Web
{
    public partial class reader : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            # region 页面传值分析
            string rID;
            int vID;
            rID = Request.QueryString["ID"];
            if (rID == null || rID.IndexOf(",", 0) > 0 || AdinZ.Common.DataValidate.IsNumber(rID) == false)
            {
                Response.Write("<H3>此页以被删除,如有疑问请联系管理员.</H3>");
                Response.End();
            }
            vID = Convert.ToInt32(rID);

            string rPage;
            rPage = Request.QueryString["page"];
            int vPage = 1;
            if (rPage != string.Empty && rPage != null && AdinZ.Common.DataValidate.IsNumber(rPage))
            {
                vPage = Convert.ToInt32(rPage);
            }

            #endregion

            #region 获取页面信息
            string vHtml = "";
            bool vWebPageCache = AdinZ.Common.ConfigHelper.GetConfigBool("WebPageCache"); //判断是否启用了页面缓存
            if (vWebPageCache == false)
            {
                vHtml = Fun_GetContent(vID, vPage);
            }
            else
            {
                int vCacheTime = AdinZ.Common.ConfigHelper.GetConfigInt("CacheTime");
                string contentCacheKey = AdinZ.Common.ConfigHelper.GetConfigString("CacheKey") + "_reader_" + vID;
                object contentCacheInfo = AdinZ.Common.DataCache.GetCache(contentCacheKey);

                //判断缓存是否存在
                if (contentCacheInfo == null)
                {
                    vHtml = Fun_GetContent(vID, vPage);
                    AdinZ.Common.DataCache.SetCache(contentCacheKey, (object)vHtml, DateTime.Now.AddSeconds(vCacheTime), TimeSpan.Zero);
                }
                else
                {
                    vHtml = contentCacheInfo.ToString();
                }
            }

            vHtml = Function.Fun_UrlRewriter(vHtml);
            Response.Write(vHtml);
            #endregion

        }

        private string Fun_GetContent(int byID, int byPage)
        {
            //获取当前内容信息
            Novel.Sys.BLL.novel_chapter bll_chap = new Novel.Sys.BLL.novel_chapter();
            Novel.Sys.Model.novel_chapter model_chap = new Novel.Sys.Model.novel_chapter();

            //获取当前栏目信息
            Novel.Sys.BLL.novel_sort bll_sort = new Novel.Sys.BLL.novel_sort();
            Novel.Sys.Model.novel_sort model_sort = new Novel.Sys.Model.novel_sort();

            model_chap = bll_chap.GetModel(byID);
            if (model_chap == null)
            {
                Response.Write("<H3>不存在该内容信息!</H3>");
                Response.End();
            }
            if (model_chap.Status == 0)
            {
                Response.Write("<H3>此内容还在审核中!</H3>");
                Response.End();
            }
            Novel.Sys.BLL.novel_article bll_art = new Novel.Sys.BLL.novel_article();
            Novel.Sys.Model.novel_article model_art = new Novel.Sys.Model.novel_article();
            model_art = bll_art.GetModel(model_chap.Aid);
            int Tid = model_art.Tid; //栏目ID

            model_sort = bll_sort.GetModel(Tid);
            if (model_sort == null)
            {
                Response.Write("<H3>不存在相应的栏目信息!</H3>");
                Response.End();
            }

            ////当该页面启用了跳转时,则跳转!
            //if (model.Jumpurl != null && model.Jumpurl.Length > 0)
            //{
            //    //Response.Redirect(model.Jumpurl);
            //    Response.Clear();
            //    Response.Status = "301 Moved Permanently";
            //    Response.AddHeader("Location", model.Jumpurl);
            //    Response.End();
            //}


            string vTemplateUrl = "reader.html";   //对应的模板

            DataTable dt = bll_chap.GetList("[ID] = " + byID).Tables[0];
            DataRow dr = dt.Rows[0];

            Template wt = new Template();
            wt.Load_Template(vTemplateUrl);
            wt.vPage = byPage;
            wt.vCID = Tid;


            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:sitehost}"), siteConfig.SiteHost, RegexOptions.IgnoreCase);

            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{field:title}"), model_art.Title, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{field:author}"), model_art.Author, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{field:bookid}"), model_art.ID.ToString(), RegexOptions.IgnoreCase);

            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{field:text}"), BookText.GetBookText(model_art.Local==1?siteConfig.FileHost+model_chap.Articlepath:model_chap.Articlepath), RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{field:status}"), model_art.Isfull == 1?"完结":"连载中...", RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{chapter:id}"), model_art.ID.ToString(), RegexOptions.IgnoreCase);

            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:filehost}"), siteConfig.FileHost, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:title}"), siteConfig.WebName, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:seotitle}"), seoConfig.SeoTitle, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:keywords}"), seoConfig.IndexKeywords, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:description}"), seoConfig.IndexDescription, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:sitepath}"), Function.Fun_GetSitePath(Tid) + " > 正文", RegexOptions.IgnoreCase);

            //替换专用标签
            Regex regex = new Regex(@"{paging:([\s\S]*?)}", RegexOptions.IgnoreCase);
            MatchCollection matchCollection = regex.Matches(wt.vContent);
            foreach (Match m in matchCollection)
            {
                string vContentTmp = "";
                string vValueKey = m.Groups[1].Value;
                switch (vValueKey.ToLower())
                {
                    case "prev":    //上一篇
                        vContentTmp = Get_Page_PrevNext(byID, model_chap.Aid, "prev");
                        break;
                    case "next":    //下一篇
                        vContentTmp = Get_Page_PrevNext(byID, model_chap.Aid, "next");
                        break;
                }
                wt.vContent = wt.vContent.Replace(m.Value, vContentTmp);
            }

            wt.vContent = wt.Parser_Tags(0, @"\{field:(.+?)\}", wt.vContent, dr);   //数据信息替换

            wt.Parser_List();
            wt.Parser_Page();
            wt.Parser_IF();
            return wt.vContent;
        }

        //获取新闻信息的上一篇,下一篇
        private string Get_Page_PrevNext(int byID, int byCID, string byFlag)
        {
            string vBaseUrl = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            string vBackInfo = "";
            string vWhere = "";
            string vOrder = "";
            string strName="";
            string strClass = "";
            switch (byFlag)
            {
                case "prev":    //上一篇
                    vWhere = "[ID] > " + byID + " AND [aid]="+byCID;
                    vOrder = "[ID] Asc";
                    strName = "下一章";
                    strClass = "class=\"prev\"";
                    break;
                case "next":    //下一篇
                    vWhere = "[ID] < " + byID + "AND [aid]="+byCID;
                    vOrder = "[ID] Desc";
                    strName = "上一章";
                    strClass = "class=\"next\"";
                    break;
            }

            Novel.Sys.BLL.novel_chapter bll = new Novel.Sys.BLL.novel_chapter();
            DataTable dt = bll.GetList(1, vWhere, vOrder).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                vBaseUrl += "?ID=" + dt.Rows[i]["ID"].ToString();
                vBackInfo = "<a href=" + vBaseUrl + " " + strClass + ">" + strName + "</a>";
            }

            return vBackInfo;
        }



    }
}